package com.zztl.infiniteBand.ui.activity

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.View
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseActivity
import com.zztl.infiniteBand.model.ServiceNoticeDatamapBean
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.ui.view.CommonItemView
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog
import com.zztl.infiniteBand.util.DataFormatUtil
import com.zztl.infiniteBand.util.PermissionUtils
import com.zztl.infiniteBand.util.UiUtils
import kotlinx.android.synthetic.main.activity_trading_detail.*

class TradingDetailActivity : BaseActivity() {

    private var mPhoneDialog: ExitDialog? = null
    private var mBean: ServiceNoticeDatamapBean? = null



    private val mPermissionGrant = object : PermissionUtils.PermissionGrant {
        override fun onPermissionGranted(requestCode: Int) {
            when (requestCode) {
                PermissionUtils.CODE_CALL_PHONE -> try {
                    showPhoneDialog()
                    //                    startActivityForResult(intent, REQUEST_CODE);
                } catch (e: Exception) {

                }

            }
        }
    }


    override fun setLayoutId(): Int =R.layout.activity_trading_detail


    override fun initBase() {
        setTitle(getString(R.string.详情))
        item_phone.setItemRightClickListener(object : CommonItemView.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                PermissionUtils.requestPermission(this@TradingDetailActivity, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant)
            }
        })
        item_phone.rightTextStr = Constans.CONTACT_PHONE_NUM
        mBean = intent.getSerializableExtra("datailData") as ServiceNoticeDatamapBean
        setUi(mBean!!)
    }


    private fun setUi(mapBean: ServiceNoticeDatamapBean) {
        mapBean.dataMap?.let { bean->
            item_id.visibility = if (TextUtils.isEmpty(bean.txid)) View.GONE else View.VISIBLE
            item_id.rightTextStr = bean.txid
            when(bean.transactionType){
                "2"->{
                    item_money.rightTextStr = "+" + DataFormatUtil.converTxtPoint4(bean.transactionMoney)
                    item_money.setLeftTextStr(getString(R.string.FIR充值))
//                    item_receive_money.rightTextStr = DataFormatUtil.converTxtPoint4(bean.transactionMoney - bean.serviceCharge)
                }
                "1"->{
                    item_money.rightTextStr = DataFormatUtil.converTxtPoint4(bean.transactionMoney)
                    item_money.setLeftTextStr(getString(R.string.FIR提币))
                }
            }
            item_receive_money.rightTextStr = DataFormatUtil.converTxtPoint4(bean.transactionMoney - bean.serviceCharge)
            item_time.rightTextStr = bean.confirmTimeStr
            item_address.rightTextStr = bean.targetAddress
            item_charge.rightTextStr = bean.serviceCharge.toString()
        }


       mapBean.title?.let { title->
           if(title == "转账成功" || title == "收款成功" || title == "系统补充奖励"){
               tv_state.text = UiUtils.getString(R.string.成功)
               tv_state.setTextColor(UiUtils.getColor(R.color.color_2ECC71))
               tv_state.setBackgroundResource(R.drawable.button_ebf9f0_bg)
           }else if(title == "转账申请已提交"){
               tv_state.text = UiUtils.getString(R.string.确认中)
               tv_state.setTextColor(UiUtils.getColor(R.color.color_2628F4))
               tv_state.setBackgroundResource(R.drawable.button_e9e9fd_bg)
           }else{
               tv_state.text = UiUtils.getString(R.string.失败)
               tv_state.setTextColor(UiUtils.getColor(R.color.color_FE1D00))
               tv_state.setBackgroundResource(R.drawable.button_ebf9f0_bg)
           }
       }

    }


    @OnClick(R.id.item_phone)
    fun click(view: View) {
        PermissionUtils.requestPermission(this@TradingDetailActivity, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant)

    }


    private fun showPhoneDialog() {
        if (null == mPhoneDialog) {
            mPhoneDialog = ExitDialog.Builder(this).setMessage(UiUtils.getString(R.string.是否拨打电话) + Constans.CONTACT_PHONE_NUM)
                    .setClickListener(object : ExitDialog.Builder.ClickListener {
                        override fun cancel() {

                        }

                        override fun comfirm() {
                            callPhone(Constans.CONTACT_PHONE_NUM)
                        }
                    }).create()
        }

        mPhoneDialog!!.show()
    }

    fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_CALL)
        val data = Uri.parse("tel:$phoneNum")
        intent.data = data
        showActivity(intent)
    }

}
